package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.persistence.model.Region;
import com.stylefeng.guns.common.persistence.model.TCooperationDot;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.service.IRegionService;
import com.stylefeng.guns.modular.system.service.ITCooperationDotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合作网点控制器
 *
 * @author fengshuonan
 * @Date 2018-01-30 21:27:26
 */
@Controller
@RequestMapping("/tCooperationDot")
public class TCooperationDotController extends BaseController {

    private String PREFIX = "/system/tCooperationDot/";

    @Autowired
    private ITCooperationDotService tCooperationDotService;


    @Resource
    private IRegionService regionService;
    /**
     * 跳转到合作网点首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tCooperationDot.html";
    }

    /**
     * 跳转到添加合作网点
     */
    @RequestMapping("/tCooperationDot_add")
    public String tCooperationDotAdd(Model model) {
        List<Region> regionList=regionService.selectListByPid("1");//查找中国下级区域，即省份信息
        //获取本企业未添加配送范围的网点
        model.addAttribute("regionList",regionList);
        return PREFIX + "tCooperationDot_add.html";
    }

    /**
     * 跳转到修改合作网点
     */
    @RequestMapping("/tCooperationDot_update/{tCooperationDotId}")
    public String tCooperationDotUpdate(@PathVariable Integer tCooperationDotId, Model model) {
        TCooperationDot tCooperationDot = tCooperationDotService.selectById(tCooperationDotId);
        model.addAttribute("item",tCooperationDot);
        List<Region> regionList=regionService.selectListByPid("1");//查找中国下级区域，即省份信息
        //获取本企业未添加配送范围的网点
        model.addAttribute("regionList",regionList);
        List<Region> city=regionService.selectListByPid(tCooperationDot.getProvinceCode()+"");
        model.addAttribute("cityList",city);
        List<Region> reg=regionService.selectListByPid(tCooperationDot.getCityCode()+"");
        model.addAttribute("regList",reg);
        LogObjectHolder.me().set(tCooperationDot);
        return PREFIX + "tCooperationDot_edit.html";
    }

    /**
     * 获取合作网点列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) Integer deptid,Integer limit, Integer offset) {
        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",deptid==null? ShiroKit.getUser().getDeptId():deptid);
        m.put("name",name);
        List<Map<String, Object>> selectList = tCooperationDotService.selectList(page,m);
        pageInfoBT.setRows(selectList);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 新增合作网点
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TCooperationDot tCooperationDot) {
        tCooperationDot.setCreateUserId(ShiroKit.getUser().getId());
        tCooperationDot.setDeptid(ShiroKit.getUser().getDeptId());

       List<TCooperationDot> list = tCooperationDotService.selectListByDotNameAndDeptId(tCooperationDot);
        if(list!=null&&list.size()>0){
            return new ErrorTip(400,"网点名称已存在");
        }
        tCooperationDotService.insert(tCooperationDot);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除合作网点
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tCooperationDotId) {
        tCooperationDotService.deleteById(tCooperationDotId);
        return SUCCESS_TIP;
    }

    /**
     * 修改合作网点
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TCooperationDot tCooperationDot) {
        tCooperationDot.setDeptid(ShiroKit.getUser().getDeptId());
        List<TCooperationDot> list = tCooperationDotService.selectListByDotNameAndDeptId(tCooperationDot);
        if(list!=null&&list.size()>0){
            return new ErrorTip(400,"网点名称已存在");
        }
        tCooperationDotService.updateById(tCooperationDot);
        return super.SUCCESS_TIP;
    }

    /**
     * 合作网点详情
     */
    @RequestMapping(value = "/detail/{tCooperationDotId}")
    @ResponseBody
    public Object detail(@PathVariable("tCooperationDotId") Integer tCooperationDotId) {
        return tCooperationDotService.selectById(tCooperationDotId);
    }
}
